package com.breeze.blockchain.controller.app;

import com.breeze.blockchain.auth.util.AppUserUtil;
import com.breeze.blockchain.controller.app.form.login.UserLoginForm;
import com.breeze.blockchain.mybatis.entity.UserDo;
import com.breeze.blockchain.service.strategy.login.LoginStrategyContext;
import com.breeze.components.web.core.ResponseDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登陆接口
 *
 * @author breeze
 */
@Slf4j
@Tag(name = "用户登陆接口")
@RestController
@RequestMapping("api/app")
public class AppLoginController {

  @Resource LoginStrategyContext loginStrategyContext;

  @Operation(summary = "用户登陆接口")
  @PostMapping("login")
  public ResponseDTO<UserDo> login(@RequestBody UserLoginForm form) {
    return ResponseDTO.ok(
        loginStrategyContext.login(
            form.mapToLoginType(), form.mobile(), form.email(), form.password(), form.code()));
  }

  @Operation(summary = "注销")
  @GetMapping("logout")
  public ResponseDTO<Void> logout() {
    AppUserUtil.logout(AppUserUtil.getLoginId());
    return ResponseDTO.ok();
  }
}
